package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.HouseType;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.HouseTag;
import com.wuming.house.service.HouseTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/web/housetag")
public class HouseTagController extends BaseController {

    @Autowired
    private HouseTagService houseTagService;


    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam Long houseId, @RequestParam(defaultValue = "1") Integer houseType) {
        try {
            List<HouseTag> houseTags = houseTagService.getHouseTagByHouseId(houseId, houseType);
            return CommonResult.createSuccessResult(houseTags, houseTags.size(), "查询所有楼盘标签成功");
        } catch (Exception e) {
            logger.error("发生业务异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询所有楼盘标签失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public JsonResult delete(@PathVariable Long id) {
        logger.info("准备删除id:{}的标签", id);
        try {
            houseTagService.removeHouseTag(id);
            return new JsonResult(null, "删除成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}", e.getMessage());
            return new JsonResult(null, "删除失败", false);
        }
    }

    @RequestMapping("/toAddTag")
    public String toAddInfo(@RequestParam Long houseId, @RequestParam(defaultValue = "1") Integer houseType, Model model) {
        model.addAttribute("houseId", houseId);
        if (HouseType.NEW_HOUSE.getValue() == houseType.intValue()) {
            return "/house/editHouseTag";
        } else {
            return "/secondhouse/editHouseTag";
        }
    }

    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(HouseTag tag) {
        try {
            houseTagService.saveHouseTag(tag);
            return new JsonResult(tag, "保存成功", true);
        } catch (Exception e) {
            logger.error("保存失败,异常:{}", e.getMessage());
            return new JsonResult(null, "保存失败", false);
        }
    }

}
